# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# Name:         demo02
# Description:  
# Author:       Administrator
# Date:         2020/2/7
#-------------------------------------------------------------------------------
from selenium import webdriver
import time


class WoniuSalesTest01:

	# 开闭原则
	def __init__(self,browser,url):

		self.driver = self.get_driver(browser)
		self.driver.maximize_window()
		self.driver.implicitly_wait(10)
		self.driver.get(url)


	# 通过get_driver方法获得一个浏览器驱动对象
	def get_driver(self,browser):
		if browser == 'Firefox':
			driver = webdriver.Firefox()
		elif browser == 'Chrome':
			driver = webdriver.Chrome()
		elif browser == 'Ie':
			driver = webdriver.Ie()
		else:
			print('浏览器名称错误，默认为火狐')
			driver = webdriver.Firefox()
		return driver

	# 登录操作
	def do_login(self,username,upassword):
		#生成一个局部的浏览器驱动对象
		driver = self.driver

		uname = driver.find_element_by_id('username')
		uname.click()
		uname.clear()
		uname.send_keys(username)

		upass = driver.find_element_by_id('password')
		upass.click()
		upass.clear()
		upass.send_keys(upassword)

		vfcode = driver.find_element_by_id('verifycode')
		vfcode.click()
		vfcode.clear()
		vfcode.send_keys('0000')

		# xpath = xml + path
		driver.find_element_by_xpath('/html/body/div[4]/div/form/div[6]/button').click()

	# 添加会员
	def add_customer(self,customerphone,customername,childsex,childdate,creditkids,creditcloth):
		driver = self.driver
		driver.find_element_by_link_text('会员管理').click()

		phone = driver.find_element_by_id('customerphone')
		phone.click()
		phone.clear()
		phone.send_keys(customerphone)

		cname = driver.find_element_by_id('customername')
		cname.click()
		cname.clear()
		cname.send_keys(customername)

		# driver.find_element_by_name(name的具体的值)
		# driver.find_element_by_class_name()
		# driver.find_element_by_tag_name()

		# 对下拉框的处理：1.随机选择一个；2.指定具体的选择项
		# 导入Select工具，它专用于处理页面上的下拉框
		# selecter = driver.find_element_by_id('childsex')
		# self.select_random(selecter)

		from selenium.webdriver.support.select import Select
		Select(driver.find_element_by_id('childsex')).select_by_visible_text(childsex)

		# 通过js注入方式将出生日期的只读属性去掉，之后再传入值
		cdate = driver.find_element_by_id('childdate')
		# document.getElementById("childdate").readOnly=false
		self.remove_readonly('childdate')
		cdate.clear()
		time.sleep(2)
		cdate.send_keys(childdate)
		time.sleep(2)

		ckids = driver.find_element_by_id('creditkids')
		ckids.click()
		ckids.clear()
		ckids.send_keys(creditkids)

		ccloth = driver.find_element_by_id('creditcloth')
		ccloth.click()
		ccloth.clear()
		ccloth.send_keys(creditcloth)

		driver.find_element_by_css_selector('button.form-control:nth-child(1)').click()

	# 验证会员添加是否成功:1.页面中是否有添加成功的提示信息；2.在列表中是否正常显示；3.在数据库中是否已添加该记录
	# 验证数据库中是否已添加的思路：（1）直接通过代码连接数据库，查看会员表中是否增加了该记录；（2）使用查询功能，如果有结果则可证明

	def verify_add_customer(self,hostname,uname,upass,dbname,customerphone,customername,childsex,childdate,creditkids,creditcloth):
		# 最简单的方式是添加之前检查一下记录数，添加之后再检查一遍，如果增加了1，则证明已添加
		# 如何查看某张表中有多少条记录
		sql = 'select count(*) from customer'
		old_result = self.query_one(hostname,uname,upass,dbname,sql)
		print(old_result[0])
		self.add_customer(customerphone,customername,childsex,childdate,creditkids,creditcloth)
		new_result = self.query_one(hostname,uname,upass,dbname,sql)
		print(new_result[0])
		if new_result[0]-old_result[0] == 1:
			print('数据库中添加成功')
		else:
			print('数据库中未添加')
	# 查询会员
	def query_customer(self,customerphone):

		pass

	# 修改会员信息
	def modify_customer(self):
		pass

	# 对下拉框进行随机元素选择
	def select_random(self,selecter):  # selecter是传递的下拉框元素
		from selenium.webdriver.support.select import Select
		seleter_length = len(Select(selecter).options)
		import random
		random_index = random.randint(0,seleter_length-1)
		Select(selecter).select_by_index(random_index)

	# 去掉某个元素的只读属性
	def remove_readonly(self,ele_id):
		self.driver.execute_script('document.getElementById("%s").readOnly=false'%(ele_id))

	# 获取数据库连接
	def getConn(self,hostname,uname,upass,dbname):
		import pymysql

		return pymysql.connect(hostname,uname,upass,dbname,charset='utf8')

	# 查询一条记录
	def query_one(self,hostname,uname,upass,dbname,sql):
		# 获取数据库连接对象
		conn = self.getConn(hostname,uname,upass,dbname)
		cursor = conn.cursor()
		cursor.execute(sql)
		result = cursor.fetchone()
		cursor.close()
		conn.close()
		# 返回一维元组
		return result

	# 查询多条记录
	def query_all(self,hostname,uname,upass,dbname,sql):
		# 获取数据库连接对象
		conn = self.getConn(hostname,uname,upass,dbname)
		cursor = conn.cursor()
		cursor.execute(sql)
		result = cursor.fetchall()
		cursor.close()
		conn.close()
		# 返回二维元组
		return result

	# 更新记录
	def update_data(self,hostname,uname,upass,dbname,sql):
		flag = False
		try:
			conn = self.getConn(hostname, uname, upass, dbname)
			cursor = conn.cursor()
			cursor.execute(sql)
			# 将数据真正提交到数据库中
			conn.commit()
			flag = True
		finally:
			cursor.close()
			conn.close()
			# 不管更新操作是否成功，都会返回真或假的结果
			return flag

if __name__ == '__main__':

	wst = WoniuSalesTest01('Firefox','http://192.168.1.4:8080/WoniuSales')
	wst.do_login('admin','admin')
	# wst.add_customer('133323232390','小红','女','2013-03-01',10,100)
	wst.verify_add_customer('192.168.1.4','root','123456','woniusales','133323232320','小红','女','2013-03-01',10,100)